package pageProcessor;

import java.util.HashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import main.ServerGlobals;
import protocol.HTTPRequest;
import protocol.HTTPResponse;
import protocol.Headers;

public class PageProcessorFactory {
	public static final String WEB_FOLDER = "WWW";
	private HashMap<String, PageProcessorInterface> fPageTypes;

	public PageProcessorFactory() {
		this.fPageTypes = new HashMap<String, PageProcessorInterface>();
		this.fPageTypes.put("jsp", new JSP());
	}

	public HTTPResponse process(HTTPRequest req) {
		String ResultString = null;

		Pattern regex = Pattern.compile("\\.(.*)$", Pattern.CASE_INSENSITIVE
				| Pattern.UNICODE_CASE | Pattern.MULTILINE);
		Matcher regexMatcher = regex.matcher(req.getfHTTPHttpRequestHeaders()
				.getfURI());
		if (regexMatcher.find()) {
			ResultString = regexMatcher.group(1);
		}
		if (null == ResultString) {
			return new HTTPResponse(Headers.STATUS_404, Headers.HTTP_VERSION,
					null, "", false);
		}

		if (this.fPageTypes.containsKey(ResultString.toLowerCase())) {
			return this.fPageTypes.get(ResultString.toLowerCase()).process(req,
					ServerGlobals.getInstance());
		} else {
			return new HTTPResponse(Headers.STATUS_404, Headers.HTTP_VERSION,
					null, "", false);
		}
	}

}
